﻿@inject IHttpService _httpService


@inject NavigationManager NavigationManager
@inject IPopupService PopupService


@if (string.IsNullOrWhiteSpace(Model.Name) == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="@("编辑 " + Model.Name)"/>
}
else
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip/>

}

<AutoSaveTip TItem="EditEntryWebsiteViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />

<div>
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.EditSummaryTip Type="ExaminedNormalListModelType.Entry" Class="mb-3" />

    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        @if (Model.Type == EntryType.Staff)
        {
            <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert Type="AlertTypes.Error" Class="mb-3">
                这个词条可能涉及到现实人物，请勿过分较真，也请谨慎编辑，仔细斟酌词句
            </CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaAlert>
        }

        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="@($"编辑 {Model.Name}（Id:{Model.Id}）")" Icon="@IconType.Entry.ToIconString()" Outline>
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Entries.WebsiteCard Model="Model" IsCollapsed="false" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
        </CnGalWebSite.Components.Containers.MasaTitleContainer>

    }
</div>


@code {
    [Parameter]
    public string Id { get; set; }
    public EditEntryWebsiteViewModel Model { get; set; } = new EditEntryWebsiteViewModel();
    bool isReady = false;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    AutoSaveTip<EditEntryWebsiteViewModel> autoSave { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                //获取词条信息
                Model = await _httpService.GetAsync<EditEntryWebsiteViewModel>("api/entries/editwebsite/" + Id);

                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取编辑词条专题页样式模板数据失败");
            }

        }
    }

    private async Task OnSubmit()
    {
        try
        {
            var obj = await _httpService.PostAsync<EditEntryWebsiteViewModel, Result>("api/entries/editwebsite", Model);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("保存词条专题页样式模板数据失败", obj.Error);
            }
            else
            {
                await autoSave.Clean();

                await PopupService.ToastSuccessAsync("保存成功", "保存词条专题页样式模板数据成功，等待审核通过");
                NavigationManager.NavigateTo("/entries/index/" + Model.Id);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "保存词条专题页样式模板数据失败");
        }
    }

    private void DataLoaded(EditEntryWebsiteViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();

    }

}
